<template>
    <div class="container">
        <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px"
                 class="demo-ruleForm">
            <el-form-item label="电话" prop="p1">
                <el-input type="text" v-model="ruleForm2.p1" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="昵称" prop="p2">
                <el-input type="text" v-model="ruleForm2.p2" auto-complete="off"></el-input>
            </el-form-item>
            <br>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {quillEditor} from 'vue-quill-editor';
    import pub from '../../utils/pub'
    import md5 from 'md5'
    import upload from '@/components/common/Upload'

    export default {
        components: {
            quillEditor,
            upload,
        },
        data: function () {
            var v1 = (rule, value, callback) => {
                if (!this.config.isMobile(value)) {
                    callback(new Error('请输入正确的手机号'));
                } else {
                    if(this.uid){
                        callback();
                    }else{
                        this.regMobile(value);
                        callback();
                    }
                }
            };
            var v2 = (rule, value, callback) => {
                if (!value) {
                    callback(new Error('请输入昵称'));
                } else if (value < 2 || value.length > 20) {
                    callback(new Error('2-20位'));
                } else {
                    callback();
                }
            };
            return {
                roleList: [],
                isHasManager: false,
                admin: {},
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                ruleForm2: {
                    p1: '', // 电话
                    p2: '', // 姓名
                },
                rules2: {
                    p1: [
                        {required: true, validator: v1, trigger: 'blur'}
                    ],
                    p2: [
                        {required: true, message: '请输入昵称', validator: v2, trigger: 'blur'}
                    ],
                }
            }
        },
        props: ['uid', 'mobile', 'nickName'],
        methods: {
            loadopen() {
                this.loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.2)'
                });
                setTimeout(() => {this.loading.close();}, 5000);
            },
            loadclose() {
                this.loading.close();
            },
            // 查询电话是否重复
            regMobile(mobile){
                this.config.post('/supply/admin/findUser', {mobile: mobile}).then(res=>{
                    if(res.data.code==='0'){
                        this.isHasManager = false;
                    }else {
                        if(res.data.code==='1'){
                            let user = res.data.user;
                            this.ruleForm2.p1 = user.name;
                            this.ruleForm2.p2 = user.mobile;

                        }else if(res.data.status==='-1001'){
                            this.ruleForm2.p1 = '';
                            this.isHasManager = false;
                            this.$message.error(this.config.handleError(res.data));
                        }else{
                            this.isHasManager = false;
                            this.$message.error(this.config.handleError(res.data));
                        }
                    }
                });
            },
            // 获取管理员信息
            getInfo() {
                this.clear();
                if(!this.uid) {
                    this.ruleForm2.p1 = '';
                    this.ruleForm2.p2 = '';
                    return;
                }
                this.ruleForm2.id = this.uid;
                this.ruleForm2.p1 = this.mobile;
                this.ruleForm2.p2 = this.nickName;
                // this.loadopen();
                // this.config.get('/supply/admin/findById', {id: this.uid}).then(res => {
                //     this.loadclose();
                //     if (res.data.code === '0') {
                //         // console.log(res.data, '获取角色信息');
                //         this.admin = res.data.admin;
                //         let user = res.data.user;
                //         this.ruleForm2.id = user.id;
                //         this.ruleForm2.p1 = user.mobile;
                //         this.ruleForm2.p2 = user.name;
                //     } else {
                //         this.$message.error(this.config.handleError(res.data));
                //     }
                // });
            },
            // 清空表单
            clear(){
                this.ruleForm2.p1 = '';
                this.ruleForm2.p2 = '';
            },
            // 获取上传文件控件返回的地址
            upCallback(url){
                this.ruleForm2.p7 = 'https://dlhz.oss-cn-beijing.aliyuncs.com/'+url;
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let obj = {
                            'id': this.ruleForm2.id,
                            'mobile': this.ruleForm2.p1,
                            'nickName': this.ruleForm2.p2,
                        };
                        // console.log(obj);
                        let url = this.uid ? '/center/user/update' : '/center/user/save';
                        this.loadopen();
                        this.config.post(url, obj).then(res => {
                            this.loadclose();
                            if (res.data.status === 1) {
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(this.config.handleError(res.data));
                            }
                            this.$emit('done', '');
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
        },
        mounted() {
        }
    }
</script>
